﻿using System;
using System.BLL;
using System.Collections.Generic;
using System.Dto;
using System.IBLL;
using System.Linq;
using System.MVC.Models;
using System.Web;
using System.Web.Mvc;

namespace System.MVC.Controllers
{
    /// <summary>
    /// 角色管理和员工管理页面控制器
    /// </summary>
    public class StaffController : Controller
    {
        /// <summary>
        /// 员工页面
        /// </summary>
        /// <returns></returns>
        public ActionResult StaffList()
        {
            return View();
        }
        /// <summary>
        /// 员工页面表格数据请求接口
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="StaffName">员工名</param>
        /// <param name="Sex">性别</param>
        /// <param name="DepartmentRoleId">角色ID</param>
        /// <returns></returns>
        public JsonResult GetStaffList(int limit, int page, string StaffName, string Sex, string DepartmentRoleId)
        {
            ISystemManager manager = new SystemManager();
            int datalet;
            var data = manager.GetAllStaffList(limit,page,StaffName,Sex,DepartmentRoleId ,out datalet);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 添加员工页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddStaff()
        {

            return View();
        }
        
        /// <summary>
        /// 添加员工信息接口
        /// </summary>
        /// <param name="addStaffView">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddStaff(AddStaffViewModel addStaffView)
        {
            ISystemManager manager = new SystemManager();
            manager.AddNewStaff(addStaffView.StaffName,addStaffView.PassWord,addStaffView.Sex,addStaffView.DepartmentRoleId,addStaffView.Wages);
            manager.AddLogs("员工操作", $"新增了{addStaffView.StaffName}员工", Session["UserName"].ToString());
            return Json(new { code = 0, msg = "添加成功"});
        }

        /// <summary>
        /// 获取到角色表的下拉框的角色数据
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="DepartmentRoleName">角色名</param>
        /// <returns></returns>
        public JsonResult GettableSelectStaffList(int limit, int page, string DepartmentRoleName) 
        {
            ISystemManager manager = new SystemManager();
            int datalet;
            var data = manager.GettableSelectStaffList(limit,page,DepartmentRoleName,out datalet);
            return Json(new { code = 0, msg = "获取成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 员工页面数据表的删除按钮的接口
        /// </summary>
        /// <param name="id">员工ID</param>
        /// <returns></returns>
        public JsonResult DelStaff(int id)
        {
            ISystemManager systemManagerScv = new SystemManager();
            systemManagerScv.DelStaffById(id);
            systemManagerScv.AddLogs("员工操作", $"删除了ID为{id}的员工", Session["UserName"].ToString());
            return Json(new { code = 0, msg = "删除成功" }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 员工修改页面
        /// </summary>
        /// <param name="id">员工ID</param>
        /// <returns></returns>
        public ActionResult EditStaff(int id)
        {
            ISystemManager manager = new SystemManager();
            var data = manager.GetOneStaffById(id);
            return View(data);
        }
        /// <summary>
        /// 员工修改页面修改后请求的接口
        /// </summary>
        /// <param name="listDto">数据模型</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditStaff(AllStaffListDto listDto)
        {
            ISystemManager manager = new SystemManager();
            manager.EditOneStaff(listDto);
            manager.AddLogs("员工操作", $"修改了ID为{listDto.Id}的员工", Session["UserName"].ToString());
            return Json(new { code = 0, msg = "修改成功" });
        }


        /// <summary>
        /// 角色管理表页面
        /// </summary>
        /// <returns></returns>
        public ActionResult DepartmentRoleList() 
        {
            return View();
        }
        /// <summary>
        /// 角色获取页面
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="DepartmentRoleName">角色名筛选</param>
        /// <returns></returns>
        public JsonResult GetDepartmentRoleList(int limit, int page, string DepartmentRoleName)
        {
            ISystemManager manager = new SystemManager();
            int datalet;
            var data = manager.GetAllDepartmentRoleList(limit, page, DepartmentRoleName, out datalet);
            return Json(new { code = 0, msg = "修改成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 分配权限页面
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <param name="DepartmentRoleName">角色名</param>
        /// <returns></returns>
        public ActionResult FenDepartmentRole(int id,string DepartmentRoleName)
        {
            ViewBag.DepartmentRoleName = DepartmentRoleName;
            ViewBag.id = id;
            ISystemManager manager = new SystemManager();
            var data = manager.GitDepartmentRoleToMenus(id);
            ViewBag.data = data;
            return View();
        }

        public JsonResult GetListMenus(int id)
        {
            ISystemManager manager = new SystemManager();
            var data = manager.GitDepartmentRoleToMenus(id);
            return Json(data,JsonRequestBehavior.AllowGet);
        }

        //string[] data = new string[100];
        /// <summary>
        /// 分配权限页面请求接口   理想是
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <param name="like"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult FenDepartmentRole(string id, string like,string DepartmentRoleName)
        {
            int Id = int.Parse(id);
            ISystemManager manager = new SystemManager();
            manager.AssignPermissions(Id, like, DepartmentRoleName);
            //manager.AddLogs("权限操作",$"对{DepartmentRoleName}重新分配权限",Session["UserName"].ToString());
            return Json(new { code = 0, msg = "修改成功" });
        }

        /// <summary>
        /// 添加角色页面
        /// </summary>
        /// <returns></returns>
        public ActionResult AddDepartmentRole()
        {
            return View();
        }
        /// <summary>
        /// tableselect 下拉选择框的数据接口
        /// </summary>
        /// <param name="limit">条数</param>
        /// <param name="page">页数</param>
        /// <param name="MenusName">菜单名筛选</param>
        /// <returns></returns>
        public JsonResult GetMenusList(int limit, int page, string MenusName)
        {
            ISystemManager manager = new SystemManager();
            int datalet;
            var data = manager.GetMenusListToSelects(limit,page,MenusName,out datalet);

            return Json(new { code = 0, msg = "修改成功", count = datalet, data = data }, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 添加角色页面数据提交接口，向数据库添加角色并分配权限
        /// </summary>
        /// <param name="Title">角色名</param>
        /// <param name="Pid">菜单ID</param>
        /// <param name="Remark">备注</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddDepartmentRole(string Title,string Pid,string Remark)
        {
            ISystemManager manager = new SystemManager();
            manager.AddNewDeparTmentRole(Title, Remark);
            var id = manager.GetDepartmentRoleIdByName(Title);
            manager.AssignPermissions(id, Pid, Title);
            return Json(new { code = 0, msg = "添加成功" });
        }

        /// <summary>
        /// table表格删除按钮请求接口
        /// </summary>
        /// <param name="id">角色ID</param>
        /// <returns></returns>
        public JsonResult DelDepartmentRole(int id)
        {
            ISystemManager manager = new SystemManager();
            manager.DelDepartmentRoleById(id);
            return Json(new { code = 0, msg = "删除成功" });
        }

        public ActionResult EditDepartmentRole(int id)
        {
            ISystemManager manager = new SystemManager();
            var data = manager.GetOneDepartmentRoleById(id);
            return View(data);
        }
        
        [HttpPost]
        public JsonResult EditDepartmentRole(AllDepartmentRoleListDto dto)
        {
            ISystemManager manager = new SystemManager();
            manager.EditDepartmentRoleByDto(dto);
            return Json(new { code = 0, msg = "修改成功" });
        }
    }
}